<?php
namespace app\common\model;
use x\Xbase;
class GoodsEvaluate extends Xbase{


    //数量
    public function getCount($goods_id){
        //全部数量
        $num = $this->db->query("select count(id) as num from goods_evaluate where goods_id = ?",[$goods_id],1);
        //好评
        $num3 = $this->db->query("select count(*) as num from goods_evaluate where goods_id = ? and grade in (4,5)",[$goods_id],1);
        //中评
        $num2 = $this->db->query("select count(*) as num from goods_evaluate where goods_id = ? and grade in (3)",[$goods_id],1);
        //差评
        $num1 = $this->db->query("select count(*) as num from goods_evaluate where goods_id = ? and grade in (1,2)",[$goods_id],1);
        $data = [
            'allnum'=>$num['num'],
            'goodsnum'=>$num3['num'],
            'generalnum'=>$num2['num'],
            'badnum'=>$num1['num'],
        ];
        return $data;
    }

    //列表详情
    public function listsinfo($map = [], $order = 'order by a.id desc', $page = 1, $limit = 20){
        $w = where($map);
        $sql = "
            select
                a.*,b.username,c.title as goods_title,d.title as attr_title,e.order_no
            from
                goods_evaluate a
            left join user b on a.user_id = b.id
            left join goods c on c.id = a.goods_id
            left join goods_price d on d.id = a.goods_price_id
            left join order_goods e on e.id = a.order_goods_id
        $w[0] {$order} " . limit($page, $limit);
        $rs = $this->db->query($sql, $w[1], '', true);
        return $rs;
    }
}
